package com.lenovo.spider.component.duplicate;

import cn.hutool.crypto.SecureUtil;
import com.lenovo.spider.component.TaskDuplicateChecker;
import com.lenovo.spider.domain.Task;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author <a href="mailto:liuhan3@lenovo.com">HanL(liuhan3)</a>
 * on 2018/4/13 11:01
 */
public class RedisTaskDuplicateChecker implements TaskDuplicateChecker {
    public static final String DUPLICATE_SET_PREFIX = "dup_";


    private final JedisPool jedisPool;

    public RedisTaskDuplicateChecker(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    @Override
    public boolean isDuplicate(Task task) {
        String setName = getDuplicateSet(task.getSiteConfig().getIdentity());
        String url = task.getRequest().getUrl();

        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.sadd(setName, SecureUtil.sha1(url)) > 0;
        }
    }

    public String getDuplicateSet(String siteIdentity) {
        return DUPLICATE_SET_PREFIX.concat(siteIdentity);
    }
}
